﻿using jlynet.core.Library;
using jlynet.core.notebook.model;
using jlynet.core.notebook.util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace jlynet.core.notebook.myform
{
    public partial class AddEntryForm : Form
    {
        List<Point> m_lsPoints;
        DirectoryDataTableModel model;

        public AddEntryForm()
        {
            InitializeComponent();
        }

        public AddEntryForm(DirectoryDataTableModel model)
        {
            InitializeComponent();
            this.MinimizeBox = false;  //去掉最小化
            this.MaximizeBox = false;  //去掉最大化
            m_lsPoints = new List<Point>();
            buttonProcess();
            this.model = model;
            if (this.model.entryDataTableModel.id != null)
                setViewDetails(this.model.entryDataTableModel.id);
            else
                this.button_add.Text = "添加";
        }

        /// <summary>
        /// 处理按钮
        /// </summary>
        private void buttonProcess()
        {
            // PictureBoxSizeMode.Zoom //图像大小按其原有的大小比例被增加或减少
            // PictureBoxSizeMode.StretchImage //PictureBox 中的图像被拉伸或收缩，以适应PictureBox的大小。
            // PictureBoxSizeMode.AutoSize //调整PictureBox的大小，使其等于所包含图像的大小
            pictureBox_copy.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox_copy.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox_copy.Image = UserResource.copy5;
            //处理鼠标点击事件
            //pictureBox_copy.MouseDown += (s,e) => {
            //    Graphics g = ((PictureBox)s).CreateGraphics();
            //    g.FillEllipse(Brushes.Black, e.X, e.Y, 4, 4);
            //    Point pt = new Point(e.X, e.Y);
            //    m_lsPoints.Add(pt);
            //    g.Dispose();
            //};
            //处理Paint事件
            //pictureBox_copy.Paint += (s,e) => {
            //    Graphics g = e.Graphics;
            //    foreach (Point pt in m_lsPoints)
            //    {
            //        g.FillEllipse(Brushes.Black, pt.X, pt.Y, 4, 4);
            //    }
            //    g.Dispose();
            //};

            pictureBox_copy1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox_copy1.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox_copy1.Image = UserResource.copy5;

            pictureBox_copy2.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox_copy2.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox_copy2.Image = UserResource.copy5;

            pictureBox_copy3.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox_copy3.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox_copy3.Image = UserResource.copy5;

            pictureBox_generate.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox_generate.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox_generate.Image = UserResource.generate;

            pictureBox_jump.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox_jump.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox_jump.Image = UserResource.jump2;

            pictureBox_sendEmail.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox_sendEmail.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox_sendEmail.Image = UserResource.send_email3;
        }

        private void setViewDetails(string id)
        {
            if (id != null)
            {
                Dictionary<string, object> dic = ListViewHelper.viewDetails(id);
                this.textBox_title.Text = (string)dic["title"];
                this.textBox_username.Text = (string)dic["username"];
                this.textBox_password_.Text = (string)dic["password_"];
                this.textBox_link.Text = (string)dic["link"];
                this.textBox_mailbox.Text = (string)dic["mailbox"];
                this.richTextBox_remarks.Text = (string)dic["remarks"];
                this.button_add.Text = "修改";
            }
            else
            {
                this.button_add.Text = "添加";
            }
        }


        private void button5_Click(object sender, EventArgs e)
        {
            //Dictionary<string, object> dic_Tag = (Dictionary<string, object>)this.Tag;

            if (!StringHelper.IsNullOrEmpty(this.textBox_username) && !StringHelper.IsNullOrEmpty(this.textBox_password_.Text))
            {
                if (!StringHelper.IsNullOrEmpty(this.textBox_link.Text) && !Tools.IsUrl(this.textBox_link.Text))
                {
                    MessageBox.Show("网址格式不正确！");
                    return;
                }
                if (!StringHelper.IsNullOrEmpty(this.textBox_mailbox.Text) && !Tools.IsEmail(this.textBox_mailbox.Text))
                {
                    MessageBox.Show("邮箱格式不正确！");
                    return;
                }

                long id = AddData();
                this.Dispose();
                //this.Close();

                //new listViewHelper().getListView("listView_entryData", new MiniPasswordBoxForm());
                //ListView listView = (ListView)dic_Tag["listView"];
                //string directory_id = (string)dic_Tag["directory_id"];

                //listViewHelper.listViewData(listView, directory_id);
            }
            else
            {
                MessageBox.Show("用户名与密码是必填项！");
                return;
            }



        }

        private long AddData()
        {
            //string filePath = (string)dic_Tag["User_FilePath"];
            //string directory_id = (string)dic_Tag["directory_id"];
            //bool isEdit = (bool)dic_Tag["isEdit"];
            //SQLiteConfigHelper.DatabaseFile = filePath;
            bool isEdit = model.isEdit;
            SQLiteConfigHelper.tableName = "entryDataTable";
            DateTime dt = DateTime.Now;
            int time = TimeHelper.ConvertDateTimeInt(dt);

            var dic = new Dictionary<string, object>();
            dic["directory_id"] = Convert.ToInt64(model.id);
            dic["title"] = this.textBox_title.Text;
            dic["username"] = this.textBox_username.Text;
            dic["password_"] = this.textBox_password_.Text;
            dic["link"] = this.textBox_link.Text;
            dic["mailbox"] = this.textBox_mailbox.Text;
            dic["remarks"] = this.richTextBox_remarks.Text;
            dic["update_time"] = time;
            if (isEdit == true)
            {
                var where = new Dictionary<string, object>();
                //string id = (string)dic_Tag["id_"];
                where["id_"] = model.entryDataTableModel.id;
                return MySQLiteHelper.Update(dic, where);
            }
            else
            {
                dic["create_time"] = time;
                return MySQLiteHelper.Insert(dic);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void pictureBox_copy_Click(object sender, EventArgs e)
        {
            if (this.textBox_username.SelectedText != "" || this.textBox_username.Text != "")
                Clipboard.SetDataObject(this.textBox_username.Text);
        }

        private void pictureBox_copy1_Click(object sender, EventArgs e)
        {
            if (this.textBox_password_.SelectedText != "" || this.textBox_password_.Text != "")
                Clipboard.SetDataObject(this.textBox_password_.Text);
        }

        private void pictureBox_copy2_Click(object sender, EventArgs e)
        {
            if (this.textBox_link.SelectedText != "" || this.textBox_link.Text != "")
                Clipboard.SetDataObject(this.textBox_link.Text);
        }

        private void pictureBox_copy3_Click(object sender, EventArgs e)
        {
            if (this.textBox_mailbox.SelectedText != "")
                Clipboard.SetDataObject(this.textBox_mailbox.Text);
        }

        private void pictureBox_generate_Click(object sender, EventArgs e)
        {
            this.textBox_password_.Text = SysHelper.GetRandomString(12, "#$%&()");
        }

        private void pictureBox_jump_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.textBox_link.Text != "")
                    System.Diagnostics.Process.Start(this.textBox_link.Text);
            }
            catch (Exception ex) { }
        }

        private void pictureBox_sendEmail_Click(object sender, EventArgs e)
        {
            if (this.textBox_mailbox.Text != "")
            {
                //System.Text.Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                //发送地址 
                //string to = "jly@jlynet.cn";
                //主题 
                string subject = "";
                //subject = System.Web.HttpUtility.UrlEncode(subject, enc);
                //内容
                string body = "";
                // body = System.Web.HttpUtility.UrlEncode(body, enc);//
                //string cc = ""; 
                //string bcc = "";
                //打开标准的软件客户端
                //System.Diagnostics.Process.Start(string.Format("mailto:{0}?subject={1}&body={2}&cc={3}&bcc={4}", to, subject, body, cc, bcc));
                System.Diagnostics.Process.Start(string.Format("mailto:{0}?subject={1}&body={2}", this.textBox_mailbox.Text, subject, body));
            }
        }
    }
}
